#include "CurveManager.h"
#include "VertexManager.h"
#include "LineManager.h"

CCurveManager::CCurveManager()
{
	m_pVManager = NULL;
	m_pLManager = NULL;
	m_lCurve.clear();
}

CCurveManager::CCurveManager(CVertexManager* pVManager, CLineManager* pLManager)
{
	m_lCurve.clear();
	m_pVManager = pVManager;
	m_pLManager = pLManager;
}

CCurveManager::~CCurveManager()
{
	m_pLManager = NULL;
	m_pVManager = NULL;
	m_lCurve.clear();
}

void CCurveManager::AddCurve(CCurve curve)
{
	m_lCurve.push_front(curve);
}

void CCurveManager::DeleteCurve(CCurve curve)
{
	list<CCurve>::iterator it;
	for (it = m_lCurve.begin(); it != m_lCurve.end();)
	{
		if (*it == curve)
		{
			m_lCurve.erase(it);
		}
		else
		{
			it++;
		}
	}
}

CCurve CCurveManager::GetCurve(int index)
{
	int count = m_lCurve.size();
	index = (index >= 0) ? index : 0;
	index = (index < count) ? index : count - 1;
	list<CCurve>::iterator it = m_lCurve.begin();
	int i = 0;
	while (i < index)
	{
		it++;
		i++;
	}
	return (*it);
}

int CCurveManager::GetCurveCount()
{
	return m_lCurve.size();
}

bool CCurveManager::IsParallel(CCurve curve1, CCurve curve2, bool &isReversed)
{
	CVector3D vss, vmm, vee, vse, ves;
	vss = curve1.m_start->m_v3D - curve2.m_start->m_v3D;
	vmm = curve1.m_mid->m_v3D - curve2.m_mid->m_v3D;
	vee = curve1.m_end->m_v3D - curve2.m_end->m_v3D;
	vse = curve1.m_start->m_v3D - curve2.m_end->m_v3D;
	ves = curve1.m_end->m_v3D - curve2.m_start->m_v3D;

	if (vss == vmm && vmm == vee)
	{
		isReversed = false;
		return true;
	}
	else if (vse == vmm && vmm == ves)
	{
		isReversed = true;
		return true;
	}
	else
		return false;
}